package Interview;

/**
 * @Auther: leiwenkang
 * @Date: 2020/7/30 15:24
 * @Description:
 */
public class Test03切分粤语和英文 {
    public static void main(String[] args) {
        String str = "哇尼部车好 cool boy啊hello";
        System.out.println(splitStr(str));
        System.out.println(isLetter('哇'));
    }

    public static String splitStr(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }

        StringBuilder sb = new StringBuilder();
        boolean isLetterBefore = false;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (isLetter(ch) || ch == ' ') {
                sb.append(ch);
                isLetterBefore = true;
            } else {
                if (isLetterBefore) {
                    sb.append(" ");
                    isLetterBefore = false;
                }
                sb.append(ch);
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    private static boolean isLetter(char ch) {
        return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
    }
}